//201902104003邵潇漩
package bysj.service;

import bysj.dao.ActorDao;
import bysj.dao.StudentDao;
import bysj.dao.UserDao;
import bysj.domain.Student;
import bysj.domain.Teacher;
import bysj.domain.User;
import bysj.exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class StudentService {
    private static StudentDao studentDao= StudentDao.getInstance();
    private static StudentService departmentService=new StudentService();
    private StudentService(){}

    public static StudentService getInstance(){
        return departmentService;
    }

    public Collection<Student> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Collection<Student> students = studentDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return students;
    }
    public Collection<Student> findAll(Teacher teacher, Pagination pagination) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Collection<Student> departments = studentDao.findAll(connection,teacher,pagination);
        JdbcHelper.close(connection);
        return departments;
    }
    public Student find(Integer id) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Student student=studentDao.find(connection,id);
        JdbcHelper.close(connection);
        return student;
    }
    public boolean add(Student student) throws SQLException {
        //获得进行事务的连接，添加教师和添加用户都使用本连接对象
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            //添加actor对象，返回值为自动生成的id
            int actorId = ActorDao.getInstance().add(connection);
            student.setId(actorId);
            //teacherDao添加教师对象，无返回值
            studentDao.add(connection,student);
            //创建用户对象，密码和用户名取自teacher字段值no
            User user = new User(student.getNo(), student.getNo(), null, student);
            //增加用户
            added = UserDao.getInstance().add(user, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚事务中所有操作
            e.printStackTrace();
            connection.rollback();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return added;
    }
    public boolean update(Student student) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        boolean student_update=studentDao.update(connection,student);
        JdbcHelper.close(connection);
        return student_update;
    }
    public boolean delete(Integer id) throws SQLException, BysjException {
        Student student=this.find(id);
        return this.delete(student);
    }

    public boolean delete(Student student) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean student_del=studentDao.delete(connection,student);
        JdbcHelper.close(connection);
        return student_del;
    }
}
